import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/clockin/attendances.dart';
import 'package:app/src/http/bean/http_data.dart';

import '../../../config/app_data_config.dart';
import '../../../manager/user_manager.dart';
import '../../../manager/ywx_manager.dart';
import '../works_2023/purcontract_bean.dart';
import '../wroks/contract_bean.dart';
import '../wroks/search_bean.dart';
import '../ywx_limit.dart';
import 'package:decimal/decimal.dart';

import 'equipment_management_bean.dart';

///设备管理：我的工单：工单领用：五金领用记录，五金退库
///五金领用记录列表实体类
// "PickId": 1185,
// "PickCode": "L240423044",
// "AddDate": "2024-04-23T14:39:36.517",
// "UserId": 838,
// "Status": 3,
// "ROWN": 1,
// "PersonName": "王尚文"
class HardwareReturnListBean extends BaseBean {
  String? PickId = ""; //五金领用id
  String? PickCode = ""; //领料单号
  String? AddDate = "";

  ///获取申请日期
  String getAddDate() {
    return AddDate?.replaceAll("T", " ") ?? "";
  }

  String? UserId = "";
  String? ROWN = "";
  String? PersonName = ""; //申请人
  String? Status = ""; //状态

  ///获取状态名称
  static String getStatusName(BuildContext context, String? Status) {
    // print("状态名称：${_StatusName}");
    String? _StatusName = null; //状态
    getStatus(context).forEach((element) {
      if (element.key == Status) {
        _StatusName = element.value;
        return;
      }
    });
    return _StatusName ?? AppStrings.of(context).HardwareStatus00;
    // return AppStrings.of(context).HardwareStatus00; //未知状态
  }

  ///跳转按钮的名称
  String getGoName(BuildContext context) {
    // AppStrings.of(context).WithdrawalfromTreasury; //退库
    // AppStrings.of(context).WithdrawalfromTreasury2; //整单退库
    if (Status == "2" || Status == "4" || Status == "7") {
      //2:已审核；4：已领用
      return AppStrings.of(context).WithdrawalfromTreasury; //退库
    }
    if (Status == "5") {
      //5:未领用 整单退
      return AppStrings.of(context).WithdrawalfromTreasury2; //整单退库
    }
    return AppStrings.of(context).ToView; //查看
  }

  //判断是否能够退库
  static bool isReturnEnable(String? Status) {
    if (Status == "2" ||
        Status == "4" ||
        Status == "5" ||
        Status == "7" ||
        Status == "9") {
      return true;
    }
    return false;
  }

  ///判断是否整单退
  bool isReturnAll() {
    if (Status == "5") {
      //5:未领用 整单退
      return true; //整单退库
    }
    return false;
  }

  static bool isReturnAll2(String? Status) {
    if (Status == "5") {
      //5:未领用 整单退
      return true; //整单退库
    }
    return false;
  }

  ///判断条目是否能够填写退库数量
  static bool isReturnNum(String? Status) {
    if (Status == "2" || Status == "4" || Status == "7" || Status == "9") {
      //2:已审核（这个状态，很少，基本上已审核，马上就变成已领用的状态了。）；4：已领用;7退库中;9未换
      return true;
    }
    return false; //其他状态，都不能填写退库数量。
  }

  static List<RollerBean>? _Status = null;

  ///获取状态集合
  static List<RollerBean> getStatus(BuildContext context) {
    if (_Status != null && _Status!.length > 0) {
      return _Status!;
    }
    List<RollerBean> datas = [];
    datas.add(RollerBean(
        key: "0", value: AppStrings.of(context).HardwareStatus0)); //待提交
    datas.add(RollerBean(
        key: "1", value: AppStrings.of(context).HardwareStatus1)); //待审核
    datas.add(RollerBean(
        key: "2",
        value: AppStrings.of(context).HardwareStatus2)); //已审核 fixme 可以退库
    datas.add(RollerBean(
        key: "3", value: AppStrings.of(context).HardwareStatus3)); //已退回
    datas.add(RollerBean(
        key: "4",
        value: AppStrings.of(context).HardwareStatus4)); //已领用  fixme 可以退库
    datas.add(RollerBean(
        key: "5",
        value: AppStrings.of(context).HardwareStatus5)); //未领用  fixme 可以退库（整单退库）
    datas.add(RollerBean(
        key: "6", value: AppStrings.of(context).HardwareStatus6)); //已退库
    datas.add(RollerBean(
        key: "7",
        value: AppStrings.of(context)
            .HardwareStatus7)); //退库中 fixme 可以退库；领料单号为L开头的可以退，M开头的不能。我们这里目前只会出现L开头的。M开头的不会有。
    datas.add(RollerBean(
        key: "8", value: AppStrings.of(context).HardwareStatus8)); //借用
    datas.add(RollerBean(
        key: "9",
        value: AppStrings.of(context).HardwareStatus9)); //未换 fixme 可以退库
    datas.add(RollerBean(
        key: "10", value: AppStrings.of(context).HardwareStatus10)); //已换
    datas.add(RollerBean(
        key: "11", value: AppStrings.of(context).HardwareStatus11)); //以旧换新
    datas.add(RollerBean(
        key: "12", value: AppStrings.of(context).HardwareStatus12)); //待换
    // datas.add(
    //     RollerBean(key: "00", value: AppStrings.of(context).HardwareStatus00));//未知状态，不放在集合里。
    _Status = datas;
    return datas;
  }

  @override
  fromJson(Map<String, dynamic> json) {
    PickId = json["PickId"]?.toString() ?? "";
    PickCode = json["PickCode"]?.toString() ?? "";
    AddDate = json["AddDate"]?.toString() ?? "";
    UserId = json["UserId"]?.toString() ?? "";
    Status = json["Status"]?.toString() ?? "";
    ROWN = json["ROWN"]?.toString() ?? "";
    PersonName = json["PersonName"]?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return HardwareReturnListBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["PickId"] = PickId ?? "";
    map["PickCode"] = PickCode ?? "";
    map["AddDate"] = AddDate ?? "";
    map["UserId"] = UserId ?? "";
    map["Size"] = Size ?? "";
    map["Status"] = Status ?? "";
    map["ROWN"] = ROWN ?? "";
    map["PersonName"] = PersonName ?? "";
    return map;
  }
}

// {
// "PickBodyId": 1934,
// "PickId": 1185,
// "MaterCode": "WJ42023",
// "MaterName": "999999999999",
// "MaterType": "07",
// "PlanNum": 2.000000,
// "OutNum": 0.000000,
// "Remarks": "",
// "IsDel": 0,
// "Status": 3,
// "BackNum": 0.000000,
// "FromType": 0,
// "basicNum": 2.000000,
// "ManufacturerId": 0,
// "ManufacturerName": "",
// "EquipmentCode": "",
// "EquipmentName": "",
// "ReceiveDate": null,
// "ReceivePerson": null,
// "SubmitDate": null,
// "SubmitPerson": null,
// "PlanBasicNum": 2.000000,
// "Position": "",
// "OutBasicNum": 0.000000,
// "TotalFeedBasicNum": 0.000000,
// "FromEquipmentName": null,
// "size": "",
// "FromEquipmentCode": null,
// "oldMaterCode": null,
// "oldMaterName": null,
// "MaterItemRemarks": "",
// "ProductCode": null,
// "TradeCanBack": true,
// "BasicMaterName": "999999999999",
// "basicunit": "升",
// "BaseNum": 0.000,
// "packageunit": "只",
// "outnum1": null,
// "IsTradeIn": false,
// "HasTradeIn": 3,
// "backnum1": null,
// "MaterTypeName": "其他",
// "IsTradeInStr": "否",
// "HasTradeInName": ""
// }
///五金领用记录详情实体类
class HardwareReturnBean extends BaseBean {
  String BackNum2 = "0"; //fixme 自己写的申请退库数量

  String? PickBodyId = "";
  String? PickId = ""; //五金领用id
  String? MaterCode = "";
  String? MaterName = ""; //五金名称
  String? MaterType = "";
  String? PlanNum = "";
  String? OutNum = "";
  String? Remarks = "";
  String? IsDel = "";
  String? Status = "";
  String? BackNum = ""; //已退数量
  String? FromType = "";
  String? basicNum = "";
  String? ManufacturerId = "";
  String? ManufacturerName = ""; //品牌
  String? EquipmentCode = "";
  String? EquipmentName = "";
  String? ReceiveDate = "";
  String? ReceivePerson = "";
  String? SubmitDate = "";
  String? SubmitPerson = "";
  String? PlanBasicNum = ""; //申请数量
  String? Position = ""; //仓位
  String? OutBasicNum = ""; //领出数量
  String? TotalFeedBasicNum = "";
  String? FromEquipmentName = "";
  String? size = ""; //规格型号
  String? FromEquipmentCode = "";
  String? oldMaterCode = "";
  String? oldMaterName = "";
  String? MaterItemRemarks = "";
  String? ProductCode = "";
  String? TradeCanBack = "";
  String? BasicMaterName = "";
  String? basicunit = "";
  String? BaseNum = "";
  String? packageunit = "";
  String? outnum1 = "";
  String? IsTradeIn = "";
  String? HasTradeIn = "";
  String? backnum1 = "";
  String? MaterTypeName = ""; //五金种类
  String? IsTradeInStr = ""; //以旧换新物品
  String? HasTradeInName = ""; //是否已换

  ///单位数量
  String getUnitNum() {
    return "${basicNum}${basicunit}/${packageunit}";
  }

  @override
  fromJson(Map<String, dynamic> json) {
    PickBodyId = json["PickBodyId"]?.toString() ?? "";
    PickId = json["PickId"]?.toString() ?? "";
    MaterCode = json["MaterCode"]?.toString() ?? "";
    MaterName = json["MaterName"]?.toString() ?? "";
    MaterType = json["MaterType"]?.toString() ?? "";
    PlanNum = json["PlanNum"]?.toString() ?? "";
    OutNum = json["OutNum"]?.toString() ?? "";
    Remarks = json["Remarks"]?.toString() ?? "";
    IsDel = json["IsDel"]?.toString() ?? "";
    Status = json["Status"]?.toString() ?? "";
    BackNum = json["BackNum"]?.toString() ?? "";
    FromType = json["FromType"]?.toString() ?? "";
    basicNum = json["basicNum"]?.toString() ?? "";
    ManufacturerId = json["ManufacturerId"]?.toString() ?? "";
    ManufacturerName = json["ManufacturerName"]?.toString() ?? "";
    EquipmentCode = json["EquipmentCode"]?.toString() ?? "";
    EquipmentName = json["EquipmentName"]?.toString() ?? "";
    ReceiveDate = json["ReceiveDate"]?.toString() ?? "";
    ReceivePerson = json["ReceivePerson"]?.toString() ?? "";
    SubmitDate = json["SubmitDate"]?.toString() ?? "";
    SubmitPerson = json["SubmitPerson"]?.toString() ?? "";
    PlanBasicNum = json["PlanBasicNum"]?.toString() ?? "";
    Position = json["Position"]?.toString() ?? "";
    OutBasicNum = json["OutBasicNum"]?.toString() ?? "";
    TotalFeedBasicNum = json["TotalFeedBasicNum"]?.toString() ?? "";
    FromEquipmentName = json["FromEquipmentName"]?.toString() ?? "";
    size = json["size"]?.toString() ?? "";
    FromEquipmentCode = json["FromEquipmentCode"]?.toString() ?? "";
    oldMaterCode = json["oldMaterCode"]?.toString() ?? "";
    oldMaterName = json["oldMaterName"]?.toString() ?? "";
    MaterItemRemarks = json["MaterItemRemarks"]?.toString() ?? "";
    ProductCode = json["ProductCode"]?.toString() ?? "";
    TradeCanBack = json["TradeCanBack"]?.toString() ?? "";
    BasicMaterName = json["BasicMaterName"]?.toString() ?? "";
    basicunit = json["basicunit"]?.toString() ?? "";
    BaseNum = json["BaseNum"]?.toString() ?? "";
    packageunit = json["packageunit"]?.toString() ?? "";
    outnum1 = json["outnum1"]?.toString() ?? "";
    IsTradeIn = json["IsTradeIn"]?.toString() ?? "";
    HasTradeIn = json["HasTradeIn"]?.toString() ?? "";
    backnum1 = json["backnum1"]?.toString() ?? "";
    MaterTypeName = json["MaterTypeName"]?.toString() ?? "";
    IsTradeInStr = json["IsTradeInStr"]?.toString() ?? "";
    HasTradeInName = json["HasTradeInName"]?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return HardwareReturnBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["PickBodyId"] = PickBodyId ?? "";
    map["PickId"] = PickId ?? "";
    map["MaterCode"] = MaterCode ?? "";
    map["MaterName"] = MaterName ?? "";
    map["MaterType"] = MaterType ?? "";
    map["PlanNum"] = PlanNum ?? "";
    map["OutNum"] = OutNum ?? "";
    map["Remarks"] = Remarks ?? "";
    map["IsDel"] = IsDel ?? "";
    map["Status"] = Status ?? "";
    map["BackNum"] = BackNum ?? "";
    map["FromType"] = FromType ?? "";
    map["basicNum"] = basicNum ?? "";
    map["ManufacturerId"] = ManufacturerId ?? "";
    map["ManufacturerName"] = ManufacturerName ?? "";
    map["EquipmentCode"] = EquipmentCode ?? "";
    map["EquipmentName"] = EquipmentName ?? "";
    map["ReceiveDate"] = ReceiveDate ?? "";
    map["ReceivePerson"] = ReceivePerson ?? "";
    map["SubmitDate"] = SubmitDate ?? "";
    map["SubmitPerson"] = SubmitPerson ?? "";
    map["PlanBasicNum"] = PlanBasicNum ?? "";
    map["Position"] = Position ?? "";
    map["OutBasicNum"] = OutBasicNum ?? "";
    map["TotalFeedBasicNum"] = TotalFeedBasicNum ?? "";
    map["FromEquipmentName"] = FromEquipmentName ?? "";
    map["size"] = size ?? "";
    map["FromEquipmentCode"] = FromEquipmentCode ?? "";
    map["oldMaterCode"] = oldMaterCode ?? "";
    map["oldMaterName"] = oldMaterName ?? "";
    map["MaterItemRemarks"] = MaterItemRemarks ?? "";
    map["ProductCode"] = ProductCode ?? "";
    map["TradeCanBack"] = TradeCanBack ?? "";
    map["BasicMaterName"] = BasicMaterName ?? "";
    map["basicunit"] = basicunit ?? "";
    map["BaseNum"] = BaseNum ?? "";
    map["packageunit"] = packageunit ?? "";
    map["outnum1"] = outnum1 ?? "";
    map["IsTradeIn"] = IsTradeIn ?? "";
    map["HasTradeIn"] = HasTradeIn ?? "";
    map["backnum1"] = backnum1 ?? "";
    map["MaterTypeName"] = MaterTypeName ?? "";
    map["IsTradeInStr"] = IsTradeInStr ?? "";
    map["HasTradeInName"] = HasTradeInName ?? "";
    return map;
  }
}

///五金退库提交数据
class HardwareReturnSubData extends BaseBean {
  EquipmentInfo? equipmentInfo;
  HardwareReturnListBean? hardwareReturnListBean;

  List<HardwareReturnBean>? datas;

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    // PickBodyId = json["PickBodyId"]?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return HardwareReturnSubData();
  }

  String _BackNum = "";
  String _BodyId = "";

  getBackNumAndBodyId() {
    _BackNum = "";
    _BodyId = "";
    datas?.forEach((element) {
      var BackNum2 = element.BackNum2?.trim() ?? "0";
      var BodyId = element.PickBodyId ?? "";
      if (StringUtils.isEmpty(BackNum2)) {
        BackNum2 = "0";
      }
      if (StringUtils.isEmpty(_BackNum)) {
        _BackNum = BackNum2;
        _BodyId = BodyId;
      } else {
        _BackNum += ",$BackNum2";
        _BodyId += ",$BodyId";
      }
    });
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    //五金单个退库数据结构：{ FactoryId:1, BackNum:"0,0,1,0", BodyId:"100,101,102,103" }
    map["FactoryId"] = AppDataConfig.getFactory()?.FactoryId ?? "";
    getBackNumAndBodyId();
    map["BackNum"] = _BackNum;
    map["BodyId"] = _BodyId;
    return map;
  }
}
